﻿/*
LICENCE___________________________________________________________________
    Copyright (C) 2012 Reverdy Sylvain

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
FIN LICENCE______________________________________________________________
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Merise_designer.UItools;

namespace Merise_designer.Objets
{
    public class CL_DictionnaireDeDonnees : IComparable<CL_DictionnaireDeDonnees>
    {
        public const String NULL = "null";
        public const String NOT_NULL = "not null";

        public String vide{ get; set; }
        public int id { get; set; }
        public String nom { get; set; }
        public String code { get; set; }
        public int idType { get; set; }
        public String stringType { get; set; }
        public String idAsso { get; set; }
        public String idEntite { get; set; }
        public Boolean pk { get; set; }
        public Boolean O { get; set; }
        public String stringO { get; set; }
        public Boolean isForeignKey { get; set; }

        public CL_DictionnaireDeDonnees(int id,int idType,String stringType, Objets.CL_Entite entite)
        {
            this.vide = "";
            this.id = id;
            this.nom = "Indefinit";
            this.code = "INDEFINIT";
            this.idEntite = entite.Id;
            this.idType = idType;
            this.stringType = stringType;
            this.pk = false;
            this.O = false;
            this.stringO = NULL;
            this.isForeignKey = false;
        }
        public CL_DictionnaireDeDonnees(int id, int idType, String stringType, Objets.CL_Association asso)
        {
            this.vide = "";
            this.id = id;
            this.nom = "Indefinit";
            this.code = "INDEFINIT";
            this.idAsso = asso.Id;
            this.idType = idType;
            this.stringType = stringType;
            this.pk = false;
            this.O = false;
            this.stringO = NULL;
            this.isForeignKey = false;
        }
        public CL_DictionnaireDeDonnees(int id, String nom, String code, int idType, String stringType, String idAsso, String idEntite, Boolean pk, Boolean req, String stringO, Boolean isForeignKey)
        {
            this.vide = "";
            this.id = id;
            this.nom = nom;
            this.code = code;
            this.idType = idType;
            this.idAsso = idAsso;
            this.idEntite = idEntite;
            this.pk = pk;
            this.stringType = stringType;
            this.O = req;
            this.stringO = stringO;
            this.isForeignKey = isForeignKey;
        }

        public object Clone()
        {
            return this.MemberwiseClone();
        }

        public int CompareTo(CL_DictionnaireDeDonnees other)
        {
            return id.CompareTo(other.id);
        }

        public static Comparison<CL_DictionnaireDeDonnees> idComparaison =
         delegate(CL_DictionnaireDeDonnees d1, CL_DictionnaireDeDonnees d2)
         {
             if (d1.isForeignKey && !d2.isForeignKey) return 1;
             if (d1.isForeignKey && d2.isForeignKey) return 0;
             if (d2.isForeignKey && !d1.isForeignKey) return -1;
             return d1.id.CompareTo(d2.id);
         };

        //public static Comparison<CL_DictionnaireDeDonnees> idAssociationComparaison =
        //  delegate(CL_DictionnaireDeDonnees d1, CL_DictionnaireDeDonnees d2)
        //  {
        //      if (d1.isForeignKey && !d2.isForeignKey) return 1;
        //      if (d1.isForeignKey && d2.isForeignKey) return 0;
        //      if (d2.isForeignKey && !d1.isForeignKey) return -1;
        //      return d1.id.CompareTo(d2.id);
        //  };

        // public static Comparison<CL_DictionnaireDeDonnees> idEntiteComparaison =
        //    delegate(CL_DictionnaireDeDonnees d1, CL_DictionnaireDeDonnees d2)
        //    {
        //        if (d1.EntIsNull() && !d2.EntIsNull()) return 1;
        //        if (d1.EntIsNull() && d2.EntIsNull()) return 0;
        //        if (d2.EntIsNull() && !d1.EntIsNull()) return -1;
        //        return d1.id.CompareTo(d2.id);
        //    };

        public Boolean EntIsNull()
        {
            return (idEntite == null || idEntite == "");
        }
        public Boolean AssoIsNull()
        {
            return (idAsso == null || idAsso == "");
        }
    }
}
